<?php

/**
 * 权限控制器
 * @author yanghao <yh38615890@sina.cn>
 * @date 07-04-12
 * @copyright (c) gandianli
 */

namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use com\gdl\newagk\interfaces\BulletinServiceClient;
use com\gdl\newagk\dataobject\bulletin\BulletinRequestDTO; 
use com\gdl\newagk\dataobject\bulletin\BulletinDTO;

class Bulletin extends Base
{

	public function index(){
	}
	//公告列表
	public function bulletinList(){

		$filename['reg'] = $this->request->param('reg') ? getStr($this->request->param('reg'), 'string') : '';
		$page = $this->request->param('p')>1 ? $this->request->param('p/d') : 1;
		$pagesize = $this->request->param('num')>0 ? $this->request->param('num/d') : 10;
		$title = $this->request->param('title') ? getStr($this->request->param('title'), 'string') : '';
		$status = $this->request->param('status') ? getStr($this->request->param('status'), 'string') : '';

		$filename['plaform'] = $this->request->param('plaform') ? getStr($this->request->param('plaform'), 'string') : '';
		if(empty($filename['reg'])){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		
		$filename['title'] = $title;
		$filename['status'] = $status;
		$filename['page'] = $page;
		$filename['pagesize'] = $pagesize;
		
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new BulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new BulletinRequestDTO();
				$engprojectDo->filename = $filename;
			
				$result = $api->bulletinlist($engprojectDo);
				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>$result->data,'totalNumber'=>$result->message];
				} else {
					$data = ['msg'=>'失败','code'=>'400','data'=>[],'totalNumber'=>0];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}

			return json_encode($data);

	}

	//添加公告
	public function add(){
				$datas = $this->request->post();
				/*$datas['content'] = '123213';
				$datas['status'] = '1';
				$datas['reg'] = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
				*/ //测试用
				if (empty($datas['content']) || empty($datas['reg'])) {
					$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
					return json_encode($data);
				}
				

				$filename['content'] = getStr($datas['content'], 'string');
				$filename['status'] = getStr($datas['status'], 'int');
				$filename['reg'] = getStr($datas['reg'], 'string');
				try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new BulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new BulletinRequestDTO();
				$engprojectDo->filename = $filename;
				$result = $api->add($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>[]];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);

	}
	
	//修改公告
	public function edit(){
				$datas = $this->request->post();
				/*$datas['id'] =1 ;
				$datas['content'] = '444444';
				$datas['status'] = '0';
				$datas['reg'] = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
				*/
				 //测试用
				if (empty($datas['id']) || empty($datas['content']) || empty($datas['reg']) ) {
					$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
					return json_encode($data);
				}
				
				$id = getStr($datas['id'], 'int');
				$filename['content'] = getStr($datas['content'], 'string');
				$filename['status'] = getStr($datas['status'], 'int');
				$filename['reg'] = getStr($datas['reg'], 'string'); //题库

				try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new BulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new BulletinRequestDTO();
				$engprojectDo->filename = $filename;
				$engprojectDo->id = $id;
				$result = $api->edit($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>[]];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);

	}
	
	
	//查看公告
	public function lookOver(){
		$reg = $this->request->param('reg') ? $this->request->param('reg') : '';

		$id = $this->request->param('id') ? $this->request->param('id/d') : 0;
	/*	$reg = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
		$id = '2';*/
		if($reg=='' || !$id){
			
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		
		}

				try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new BulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new BulletinDTO();
				$engprojectDo->reg = $reg;
				$engprojectDo->id = $id;
				$result = $api->lookOver($engprojectDo);

				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>$result->data[0]];
		
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	public function bulletinDelete() 
    {
		$reg = $this->request->param('reg') ? $this->request->param('reg') : '';
		$id = $this->request->param('id') ? $this->request->param('id') : 0;
		$del = $this->request->param('del');
		if($reg=='' || !$id){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		if($del == 'deletes'){
			$filename['isdelete'] = 1;
		} else if($del == 'removes'){
			$filename['isdelete'] = 'removes';
		} else if($del == 'reduction'){
			$filename['isdelete'] = 0                ;
		} else {
			$data = ['msg'=>'参数错误','code'=>'500'];
			return json_encode($data);
		}
	try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
           	$api = new BulletinServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new BulletinRequestDTO();
            $engprojectDo->filename = $filename;
			$engprojectDo->id = $id;
			$engprojectDo->reg = $reg;
            $result = $api->examStatus($engprojectDo);
			if ($result->code) {
               $data = ['msg'=>'操作成功','code'=>'200','data'=>[]];
            } else {
               $data = ['msg'=>'操作失败','code'=>'200','data'=>[]];
            }
		} catch (Exception $ex) {
            $data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
        }
		 return json_encode($data);
    }
		public function bulletinStatus() 
    {
		$reg = $this->request->param('reg') ? $this->request->param('reg') : '';
		$id = $this->request->param('id') ? $this->request->param('id') : 0;
		if($plaform_id=='a' || !$id){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		$filename['status'] = $this->request->param('status')=='enable' ? 1 : 0;
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
           	$api = new BulletinServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new BulletinRequestDTO();
            $engprojectDo->filename = $filename;
			$engprojectDo->id = $id;
			$engprojectDo->reg = $reg;
            $result = $api->examStatus($engprojectDo);
			if ($result->code) {
               $data = ['msg'=>'操作成功','code'=>'200','data'=>[]];
            } else {
               $data = ['msg'=>'操作失败','code'=>'200','data'=>[]];
            }
		} catch (Exception $ex) {
            $data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
        }
		 return json_encode($data);
    }
	
	//app最近的公告
	public function appList(){

		$plaform_id = $this->request->param('plaformid') ? getStr($this->request->param('plaformid'), 'int') : 0;
		if(!$plaform_id){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
				$api = new BulletinServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new BulletinRequestDTO();
				$engprojectDo->plaform_id = $plaform_id;
			
				$result = $api->appList($engprojectDo);
				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>$result->data];
				} else {
					$data = ['msg'=>'失败','code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}

			return json_encode($data);

	}
	

}
